package com.camel.base.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.camel.base.model.ZsOption;
import com.camel.base.model.ZsQueue;
import com.camel.base.service.ZsOptionService;
import com.camel.base.service.ZsQueueService;
import com.camel.core.controller.BaseCommonController;
import com.camel.core.entity.Result;
import com.camel.core.exceptions.CustomException;
import com.camel.core.service.BaseCommonService;
import com.camel.core.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.List;

/**
 * @author camel
 */
@RestController
@RequestMapping("zsQueue")
public class ZsQueueController extends BaseCommonController<ZsQueue> {

    @Autowired
    private ZsQueueService<ZsQueue> service;

    @Override
    public boolean deleteAble(Serializable id) {
        ZsQueue byId = service.getById(id);
        if (ObjectUtils.isEmpty(byId)) {
            throw new CustomException("删除失败: " + getMouduleName() + "不存在");
        }
        return true;
    }

    @PostMapping("sync")
    public Result sync(@RequestBody JSONObject params) {
        JSONArray names = params.getJSONArray("queueNames");
        service.remove(new QueryWrapper<ZsQueue>());
        for (Object name : names) {
            ZsQueue zsQueue = new ZsQueue();
            zsQueue.setQueue((String) name);
            super.save(zsQueue);
        }

        return ResultUtil.success("操作成功");
    }

    @Override
    public BaseCommonService<ZsQueue> getiService() {
        return service;
    }

    @Override
    public String getMouduleName() {
        return "队列";
    }
}
